<?php

class Admins
{
	var $pdo = null;
	var $logger = null;
	function __construct()
	{
		$this->pdo = PDOWrapper::instance();
		$this->logger = CLog::getInstance();
	}

	private function CheckParams($PostArray){

		if(!isset($PostArray['token'])) {
			Utils::ShowError(ErrorDefine::SHORT_OF_TOKEN);
			return false;
		}
		$token = $PostArray['token'];
		$admin = User::GetAidByToken($token);
		if($admin === false) {
			Utils::ShowError(ErrorDefine::REQUIRED_FIELD_EMPTY);
			return false;
		}

		return $admin;
	}

	function LoginV1($GetParams, $PostArray)
	{
		$logger = CLog::getInstance();

		if(empty($PostArray)) {
			Utils::ShowError(ErrorDefine::EMPTY_POST_DATA);
			return false;
		}

		if(!isset($PostArray['mobile'])) {
			Utils::ShowError(ErrorDefine::SHORT_OF_MOBILE);
			return false;
		}

		$mobile = $PostArray['mobile'];					//获取管理员手机号
		$error_code = Common::CheckMobile($mobile);
		if(0 != $error_code) {
			Utils::ShowError($error_code);
			return false;
		}

		if(!isset($PostArray['password'])) {
			Utils::ShowError(ErrorDefine::SHORT_OF_PASSWORD);
			return false;
		}
		$password = $PostArray['password'];

		$error_code = Common::CheckPassword($password);
		if(0 != $error_code) {
			Utils::ShowError($error_code);
			return false;
		}


		$pdo = PDOWrapper::instance();

		$sql = sprintf("SELECT admin_id,password,portrait,admin_type FROM admins WHERE mobile=:mobile");
		$params = array(':mobile' => $mobile);
		$results = $pdo->query($sql, $params);
		if(false === $results) {
			Utils::ShowError(ErrorDefine::DATABASE_ERROR);
			return false;
		} else if(empty($results)) {
			Utils::ShowError(ErrorDefine::MOBILE_NOT_EXIST);
			return false;
		} else {
			$admin_id = $results[0]['admin_id'];
			$db_password = $results[0]['password'];
			$portrait = $results[0]['portrait'];//获取用户头像
			$admin_type = $results[0]['admin_type'];

			$admin_info = array();
			$admin_info['mobile'] = $mobile;
			if(empty($portrait)){
				$portrait = 'https://ss1.baidu.com/6ONXsjip0QIZ8tyhnq/it/u=1707489427,2106606795&fm=58';
			}
			$admin_info['portrait'] = $portrait;
			$crypt = new MyCrypt(CRYPT_KEY);
			$db_password = $crypt->decrypt($db_password);

			if($password != $db_password) {
				Utils::ShowError(ErrorDefine::PASSWORD_NOT_CORRECT);
				return false;
			}

			$timestamp = time();
			$format_time = strftime('%Y-%m-%d %H:%M:%S', $timestamp);
			$client_ip = Utils::GetClientIP();
			$token = $admin_id.User::TOKEN_DELIM.$admin_type.User::TOKEN_DELIM.$timestamp;
			$token = $crypt->encrypt($token);

			$params = array(
				'token' => $token,
				'last_login_ip' => Utils::IP2Num($client_ip),
				'last_login_time' => $format_time
				);
			$ret = $pdo->update('admins', $params, array('admin_id' => $admin_id));
			if(false === $ret) {
				$str = "login failed: " . $pdo->getErrorMessage();
				$log_params = array('mobile' => $mobile, 'user_type' => $PostArray['user_type']);
				$logger->warning($str, ErrorDefine::LOGIN_FAILED, $log_params);
				Utils::ShowError(ErrorDefine::LOGIN_FAILED);
				return false;
			}
			
			$data = array(
				'token' => $token,
				'utype' => $admin_type,
				'admin_info' => $admin_info
			);

			Utils::ShowMessage($data);
		}

		return true;
	}

	function LogoutV1($GetParams, $PostArray)
	{
		if(!isset($GetParams['token'])) {
			Utils::ShowError(ErrorDefine::SHORT_OF_TOKEN);
			return false;
		}

		$token = $GetParams['token'];
		$ret = User::GetAidByToken($token);

		if($ret === false) {
			Utils::ShowError(ErrorDefine::INVALID_TOKEN);
			return false;
		}

		$logger = CLog::getInstance();

		$pdo = PDOWrapper::instance();

		$wheres = array('admin_id' => $GetParams['admin_id']);
		$ret = $pdo->update('admins', array('token' => ''), $wheres);
		if(false === $ret) {
			$str = "logout failed: " . $pdo->getErrorMessage();
			$logger->warning($str, ErrorDefine::LOGOUT_FAILED, $wheres);
			Utils::ShowError(ErrorDefine::LOGOUT_FAILED);
			return false;
		}

		Utils::ShowMessage();

		return true;
	}
	
	function TokenCheckV1($GetParams, $PostArray){
	    $status = 0;
	    if(!isset($GetParams['token'])){
	        Utils::ShowError(ErrorDefine::SHORT_OF_TOKEN);
	        return false;
	    }
	    $token = $GetParams['token'];
	    $ret = User::GetAidByToken($token);
	    if ($ret){
	        $status = 1;
	    }
	     
	    $message = array(
	        'status' => $status
	    );
	     
	    Utils::ShowMessage($message);
	     
	    return true;
	}

}

?>
